#region File Description
//-----------------------------------------------------------------------------
// MenuEntry.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AfterlifeLib.Input;
#endregion

namespace AfterlifeLib.GameScreens.Menus
{
    /// <summary>
    /// Helper class represents a single entry in a MenuScreen. By default this
    /// just draws the entry text string, but it can be customized to display menu
    /// entries in different ways. This also provides an event that will be raised
    /// when the menu entry is selected.
    /// </summary>
    public class MenuEntry
    {
        static Color normalColor = Color.Gray;
        /// <summary>
        /// De kleur van de text van de MenuEntry als hij niet geselecteerd is.
        /// </summary>
        public static Color NormalColor
        {
            get { return normalColor; }
            set { normalColor = value; }
        }

        static Color selectedColor = Color.White;
        /// <summary>
        /// De kleur van de text van de MenuEntry als hij geselecteerd is.
        /// </summary>
        public static Color SelectedColor
        {
            get { return selectedColor; }
            set { selectedColor = value; }
        }

        static Vector2 valueOffset = new Vector2(500, 0);
        public static Vector2 ValueOffset
        {
            get { return valueOffset; }
            set { valueOffset = valueOffset; }
        }

        /// <summary>
        /// The text rendered for this entry.
        /// </summary>
        string text;

        /// <summary>
        /// Tracks a fading selection effect on the entry.
        /// </summary>
        /// <remarks>
        /// The entries transition out of the selection effect when they are deselected.
        /// </remarks>
        float selectionFade;

        /// <summary>
        /// Gets or sets the text of this menu entry.
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        /// <summary>
        /// Event raised when the menu entry is selected.
        /// </summary>
        public event EventHandler Selected;


        /// <summary>
        /// Method for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelectEntry()
        {
            if (Selected != null)
                Selected(this, null);
        }

        /// <summary>
        /// Maakt een nieuwe MenuEntry aan.
        /// </summary>
        /// <param name="text">De text van de MenuEntry</param>
        public MenuEntry(string text)
        {
            this.text = text;
        }

        public virtual void HandleInput(MenuScreen screen) 
        {
            if (InputState.Accept)
            {
                OnSelectEntry();
            }
            else if (InputState.Cancel)
            {
                screen.ExitScreen();
            }
        }

        /// <summary>
        /// Update de MenuEntry.
        /// </summary>
        public virtual void Update(MenuScreen screen, bool isSelected,
                                                      GameTime gameTime)
        {
            // When the menu selection changes, entries gradually fade between
            // their selected and deselected appearance, rather than instantly
            // popping to the new state.
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }


        /// <summary>
        /// Tekent een MenuEntry.
        /// </summary>
        public virtual void Draw(MenuScreen screen, Vector2 position,
                                 bool isSelected, GameTime gameTime)
        {
            //Welke kleur de MenuEntry moet zijn
            Color color = isSelected ? SelectedColor : NormalColor;
            color.A = screen.TransitionAlpha;

            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
            SpriteFont font = screen.ScreenManager.Font;

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);
            
            //Teken de text op het scherm
            spriteBatch.DrawString(font, text, position, color, 0,
                                   origin, 1f, SpriteEffects.None, 0);
        }

        /// <summary>
        /// Verkrijgt de hoogte van het lettertype.
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return screen.ScreenManager.Font.LineSpacing;
        }

        public Vector2 GetStringSize(MenuScreen screen, string s)
        {
            return screen.ScreenManager.Font.MeasureString(s);
        }
    }
}
